﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using appDataContracts;
using appClient;
using appInterfaces;
using System.ServiceModel;


namespace appClient
{
	public partial class MyCaddie : System.Web.UI.Page
	{
		IClient client;
		int key;

		protected void Page_Load(object sender, EventArgs e)
		{
			if (Session["user"] != null)
			{
				client = new ChannelFactory<IClient>("clientHttp").CreateChannel();
				int i=0;
				decimal finalTotal = 0;

				// on vérifie que le panier n'est pas vide
				if (Session["caddie"] != null && ((List<CaddieProduct>)Session["caddie"]).Count != 0)
				{
					// check de la variable "del" en url --> suppression
					if (Request.QueryString["del"] != null && ((List<CaddieProduct>)Session["caddie"])[i] != null)
					{
						((List<CaddieProduct>)Session["caddie"]).RemoveAt(i);
						if (((List<CaddieProduct>)Session["caddie"]).Count == 0)
							Order.Enabled = false;
					}

					// on liste tous les enregistrements
					foreach (CaddieProduct caddieP in ((List<appClient.CaddieProduct>)Session["caddie"]))
					{
						HtmlGenericControl tableRow = new HtmlGenericControl("tr");

						// pour chaque enregistrement, on remplit les cellules
						HtmlGenericControl tableCellProductName = new HtmlGenericControl("td");
						HtmlGenericControl tableCellPrice = new HtmlGenericControl("td");
						HtmlGenericControl tableCellQuantity = new HtmlGenericControl("td");
						HtmlGenericControl tableCellTotal = new HtmlGenericControl("td");
						tableCellProductName.InnerText = client.getProductInfo(caddieP.Id).PRODUCTNAME;
						tableCellPrice.InnerText = client.getProductInfo(caddieP.Id).PRODUCTPRICE.ToString();
						tableCellQuantity.InnerText = caddieP.Quantity.ToString();
						decimal total = caddieP.Quantity * client.getProductInfo(caddieP.Id).PRODUCTPRICE;
						tableCellTotal.InnerText = total.ToString() + " €";

						tableRow.Controls.Add(tableCellProductName);
						tableRow.Controls.Add(tableCellPrice);
						tableRow.Controls.Add(tableCellQuantity);
						tableRow.Controls.Add(tableCellTotal);



						// lien supprimer
						HtmlGenericControl tableCellDelete = new HtmlGenericControl("td");
						tableCellDelete.InnerHtml = "<a href=\"MyCaddie.aspx?del=" + i + "\" class=\"button\">Supprimer</a>";
						tableRow.Controls.Add(tableCellDelete);

						// on ajoute au tbody
						tbodyCaddie.Controls.Add(tableRow);

						finalTotal += total;

						i++;
					}

					totalPriceDiv.InnerText = "Total à payer : " + finalTotal + " €";
				}
				else
					Order.Enabled = false;
			}
			else
			{
				Response.Redirect("Default.aspx");
			}
		}

		protected void Clear_Click(object sender, EventArgs e)
		{
			((List<CaddieProduct>) Session["caddie"]).Clear();
			Session["caddie"] = new List<CaddieProduct>();
			Response.Redirect("MyCaddie.aspx");
		}

		protected void Order_Click(object sender, EventArgs e)
		{
			Response.Redirect("Order.aspx");
		}

		
	}
}